package util
{
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.LinkButton;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.core.Container;
	
	public class DoFile extends Container
	{ 
		
		private var stateText:Label;  //外面进度状态显示条
		private var linkb:LinkButton;  //外面进度状态显示条
		private var textArea:TextArea;  //外面结果文件显示框
		private var url:String = "FileUploadServlet";  //文件上传请求路径
		private var input:TextInput;
		private var img:Image;
		  
		private var fileDown:FileReference; //下载文件
		private var file:FileReference;
		
		public function DoFile()
		{
			Alert.okLabel = "是";
			super.createChildren();
		}
		
		public function upFile(stateText:Label,textArea:TextArea):void {
			file = new FileReference();
			this.stateText = stateText;
			this.textArea = textArea;
			file.addEventListener(Event.SELECT,onSelected);
			file.addEventListener(Event.COMPLETE,onCompleted);
			file.addEventListener(ProgressEvent.PROGRESS,onProgress);
			file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onUploadCompleteData);
		}
		public function upFileImage(input:TextInput,img:Image):void {
			file = new FileReference();
			this.input = input;
			this.img = img;
			file.addEventListener(Event.SELECT,onSelected);
			file.addEventListener(Event.COMPLETE,onCompletedImag);
			file.addEventListener(ProgressEvent.PROGRESS,onProgress);
			file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onUploadCompleteData2);
		}
		
		public function downFile(linkb:LinkButton,url:String):void {
			fileDown = new FileReference(); 
			this.linkb = linkb;
			linkb.label = "正在导出…";
			this.url = url;
			fileDown.addEventListener(Event.COMPLETE,onDownCompleted);
			fileDown.addEventListener(ProgressEvent.PROGRESS,onDownProgress);
			onDownLoad();
		}
		 
		internal function onDownCompleted(e:Event):void {
			var fileRef:FileReference = e.currentTarget as FileReference;
			Alert.show("导出成功，保存文件名为：" + fileRef.name,"提示");
			linkb.label = "再次导出";
		}
		
		internal function onDownProgress(evt:ProgressEvent):void
		{
			linkb.label = "已下载：" +Math.round(100*evt.bytesLoaded/evt.bytesTotal)+"%！";
		}
		
			
		public function upLoad():void {
			//文件过虑
			var txtFilter:FileFilter = new FileFilter("Images","*.jpg;*.gif;*.png");
			file.browse([txtFilter]);//选择文件
		}
		
		/*显示返回数据结果*/
		internal function onUploadCompleteData(e:DataEvent):void {
			var mesdata:String = e.data.toString();
			mesdata.replace("\r\n","\r");
			mesdata.replace("\r\r","\r");
			//去掉首行为空的情况
			if(mesdata.substring(0,1) == "\r" || mesdata.substr(0,1) == "\n") {
				mesdata = mesdata.substr(1);
			}
			textArea.text = mesdata+"\r"+textArea.text;
		}
		/*显示返回数据结果*/
		internal function onUploadCompleteData2(e:DataEvent):void {
			var mesdata:String = e.data.toString();
			input.text = mesdata;
			img.source = mesdata;
		}
		
		internal function onSelected(e:Event):void {
			input.text = "选择了文件:" + file.name;
			onUpLoad(); //上传文件
		}
		
		internal function onCompleted(e:Event):void {
			input.text = "上传完毕！";
		}
		
		internal function onCompletedImag(e:Event):void {
			input.text = "上传完毕！";
		}

		internal function onProgress(evt:ProgressEvent):void
		{
			if(evt.bytesTotal == 0) {
				evt.bytesTotal = 1;
			}
			input.text = "已上传：" +Math.round(100*evt.bytesLoaded/evt.bytesTotal)+"%";
		}
		
		/**
		*调用FileReference的实例方法upload()实现文件上传
		**/
		internal function onUpLoad():void
		{
			if(file.size>0)
			{
				input.text = "正在上传文件：" +file.name;
			}
			var request:URLRequest =  new URLRequest();
			request.url = url;
			file.upload(request);
		}
		
		/* 下载
		 * download
		 * */
	 	/**
		*调用FileReference类的实例方法download()实现文件下载
		**/
		internal function onDownLoad():void
		{
			var request:URLRequest = new URLRequest();
			request.url = url;
			fileDown.download(request);
		}

	}
}